//
//  AudioPlayerBottomView.swift
//

import UIKit

class AudioPlayerBottomView: UIView {
    
    /// 点击暂停/播放按钮
    var didClickPlayer: () -> Void = {}
    /// 拖动进度条
    var sliderValueDidChange: (Double) -> Void = { _ in }
    /// 进度条按下/松开状态
    var sliderTouch: (SliderTouch) -> Void = { _ in }
    var didNext: () -> Void = {}
    var didUp: () -> Void = {}
    var didPlayerType: (Bool) -> Void = { _ in }
    
    /// 进度条进度值
    var sliderValue: Float = 0 {
        didSet {
            slider.setValue(sliderValue, animated: true)
        }
    }
    
    /// 当前播放的时间
    var currentTime: Double = 0 {
        didSet {
            playingTimeLabel.text = currentTime.formatTime
        }
    }
    
    /// 最大时间
    var maxTime: Double = 0 {
        didSet {
            maxTimeLabel.text = maxTime.formatTime
        }
    }
    
    var playState: PlayState = .stopped {
        didSet {
            playerBtn.isSelected = playState == .playing
        }
    }
    
    private lazy var playerBtn: UIButton = {
        let view = UIButton()
        view.setImage("blh_audio_stop_music".image, for: .normal)
        view.setImage("blh_audio_play_music".image, for: .selected)
        view.addTarget(self, action: #selector(playerBtnClick), for: .touchUpInside)
        return view
    }()
    
    private lazy var nextBtn: UIButton = {
        let view = UIButton()
        view.setImage("blh_audio_next_music".image, for: .normal)
        view.addTarget(self, action: #selector(nextBtnClick), for: .touchUpInside)
        return view
    }()
    
    private lazy var upBtn: UIButton = {
        let view = UIButton()
        view.setImage("blh_audio_previous_music".image, for: .normal)
        view.addTarget(self, action: #selector(upBtnClick), for: .touchUpInside)
        return view
    }()
    
    private lazy var playerTypeBtn: UIButton = {
        let view = UIButton()
        view.setImage("blh_audio_loop".image, for: .normal)
        view.setImage("blh_audio_loop_single".image, for: .selected)
        view.addTarget(self, action: #selector(playerTypeBtnClick), for: .touchUpInside)
        return view
    }()
    
    private lazy var playerListBtn: UIButton = {
        let view = UIButton()
        view.setImage("blh_audio_list".image, for: .normal)
        return view
    }()
    
    private lazy var playingTimeLabel: UILabel = {
        let playingTimeLabel = UILabel()
        playingTimeLabel.text = "00:00"
        playingTimeLabel.textColor = UIColor.gray
        playingTimeLabel.font = UIFont.systemFont(ofSize: 12)
        playingTimeLabel.textAlignment = .center
        return playingTimeLabel
    }()
    
    private lazy var maxTimeLabel: UILabel = {
        let maxTimeLabel = UILabel()
        maxTimeLabel.text = "00:00"
        maxTimeLabel.textColor = UIColor.gray
        maxTimeLabel.font = UIFont.systemFont(ofSize: 12)
        maxTimeLabel.textAlignment = .center
        return maxTimeLabel
    }()
    
    private lazy var slider: UISlider = {
        let slider = UISlider()
        slider.minimumValue = 0
        slider.maximumValue = 1
        slider.minimumTrackTintColor = UIColor.theme
        slider.maximumTrackTintColor = UIColor.line
        slider.setThumbImage(UIImage(named: "blh_audio_dot"), for: .normal)
        slider.addTarget(self, action: #selector(sliderValueChange), for: .valueChanged)
        slider.addTarget(self, action: #selector(sliderTouchDown), for: .touchDown)
        slider.addTarget(self, action: #selector(sliderTouchUpInside), for: .touchUpInside)
        return slider
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSubviews() {
        
        addSubview(playerBtn)
        addSubview(upBtn)
        addSubview(nextBtn)
        addSubview(playerTypeBtn)
        addSubview(playerListBtn)
        addSubview(slider)
        addSubview(playingTimeLabel)
        addSubview(maxTimeLabel)
        
        playerBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(5.fit)
            make.size.equalTo(CGSize(width: 50.fit, height: 50.fit))
        }
        
        upBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(playerBtn.snp.centerY)
            make.right.equalTo(playerBtn.snp.left).offset(-50.fit)
        }
        
        nextBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(playerBtn.snp.centerY)
            make.left.equalTo(playerBtn.snp.right).offset(50.fit)
        }
        
        playerTypeBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(playerBtn.snp.centerY)
            make.left.equalToSuperview().offset(30.fit)
        }
        
        playerListBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(playerBtn.snp.centerY)
            make.right.equalToSuperview().offset(-30.fit)
        }
        
        slider.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(5.fit)
            make.left.equalToSuperview().offset(60.fit)
        }
        
        playingTimeLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(slider.snp.centerY)
            make.left.equalToSuperview().offset(space)
        }
        
        maxTimeLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(slider.snp.centerY)
            make.right.equalToSuperview().offset(-space)
        }
    }
}

extension AudioPlayerBottomView {
    
    @objc private func playerBtnClick() {
        didClickPlayer()
    }
    
    @objc private func nextBtnClick() {
        didNext()
    }
    
    @objc private func upBtnClick() {
        didUp()
    }
    
    @objc private func playerTypeBtnClick() {
        playerTypeBtn.isSelected = !playerTypeBtn.isSelected
        didPlayerType(playerTypeBtn.isSelected)
    }
    
    @objc private func sliderValueChange() {
        let value = slider.value
        sliderValueDidChange(Double(value))
    }
    
    @objc private func sliderTouchDown() {
        sliderTouch(.down)
    }
    
    @objc private func sliderTouchUpInside() {
        sliderTouch(.up)
    }
}
